<template>
  <v-dialog
    :title="title"
    :close-on-click-modal="false"
    v-model="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'" label-width="120px">
      <el-row  >
        <!-- <el-col :span="12">
            <el-form-item label="项目编号" prop="projectNo"
                :rules="[{required:true,message:'项目编号不能为空',trigger:'blur'} 
                 ]">
              <el-input v-model="inputForm.projectNo" placeholder="请填写项目编号"     ></el-input>
           </el-form-item>
        </el-col> -->
        <el-col :span="12">
            <el-form-item label="项目新建依据" prop="basisType"
                :rules="[{required:true,message:'项目新建依据不能为空',trigger:'blur'} 
                 ]">
                <el-select v-model="inputForm.basisType" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('basis_type')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="项目名称" prop="projectName"
                :rules="[{required:true,message:'项目名称不能为空',trigger:'blur'} 
                 ]">
              <el-input v-model="inputForm.projectName" placeholder="请填写项目名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="年份" prop="year"
                :rules="[{required:true,message:'项目年份',trigger:'blur'} 
                 ]">
               <el-date-picker
                v-model="inputForm.year"
                type="year"
                placeholder="请选择年份"
                format="YYYY"
                style="width:100%"     />
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="项目来源" prop="projectSource"
                :rules="[
                 ]">
                <el-select v-model="inputForm.projectSource" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('project_source')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="项目类型" prop="projectType"
                :rules="[
                 ]">
                <el-select v-model="inputForm.projectType" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('project_type')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
            <el-form-item label="项目状态" prop="projectStatus"
                :rules="[
                 ]">
                <el-select v-model="inputForm.projectStatus" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('project_status')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col> -->
        <el-col :span="12">
            <el-form-item label="集团内/外" prop="groupInternalExternal"
                :rules="[
                 ]">
                <el-select v-model="inputForm.groupInternalExternal" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('group_internal_external')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="业主单位" prop="correspondent.id"
                :rules="[
                 ]">
          <GridSelect
            title="选择业主单位"
            labelName = 'companyName'
            labelValue = 'id'
            v-model = "inputForm.correspondent.id"
       
            :columns="[
          
            {
              prop: 'companyName',
              label: '单位名称'
            },
            {
              prop: 'legalRepresentative',
              label: '法定代表人'
            }
            ]"
            :searchs="[
           
            {
              prop: 'companyName',
              label: '单位名称'
            }
            ]"
            :api="correspondentService">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="管理部门" prop="office.id"
                :rules="[{required:true,message:'管理部门不能为空',trigger:'blur'} 
                 ]">
                <office-select v-model="inputForm.office.id"/>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="负责人" prop="projectLeader.id"
                :rules="[{required:true,message:'负责人不能为空',trigger:'blur'} 
                 ]">
                <user-select :limit='1' v-model="inputForm.projectLeader.id"></user-select>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <template #footer>    
        <span class="dialog-footer">
          <el-button @click="visible = false" icon="circle-close">关闭</el-button>
          <el-button  type="primary" v-if="method != 'view'" @click="doSubmit()" icon="circle-check" v-noMoreClick>确定</el-button>
        </span>
    </template>    
  </v-dialog>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  import OfficeSelect from '@/components/officeSelect'
  import UserSelect from '@/components/userSelect'
  import projectMainService from '@/api/project/projectMainService'
  import correspondentService from '@/api/correspondent/correspondentService'   
  import moment from "moment";
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        correspondentService: correspondentService,  
        inputForm: {
          id: '',
          projectNo: '',
          basisType: '',
          projectName: '',
          projectSource: "1",
          projectType: '',
          projectStatus: 1,
          groupInternalExternal: '',
          correspondent: {
            id: ''
          },
          office: {
            id: ''
          },
          projectLeader: {
            id: ''
          },
          year:moment().format("YYYY")
        }
      }
    },
    components: {
      GridSelect,
      OfficeSelect,
      UserSelect
    },
    created () {
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建项目`
        } else if (method === 'edit') {
          this.title = '修改项目'
        } else if (method === 'view') {
          this.title = '查看项目'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            projectMainService.queryById(this.inputForm.id).then((data) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.inputForm.year=moment(this.inputForm.year).format("YYYY")
            projectMainService.save(this.inputForm).then((data) => {
              this.visible = false
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>

  
